package com.meego.common.crypto;

import com.meego.common.log.GA;
import com.meego.common.util.HexUtil;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Hash {

    private static Hash cryptoHash;

    public static Hash getInstance() {
        if (cryptoHash == null) {
            cryptoHash = new Hash();
        }
        return cryptoHash;
    }

    public String hash(String input) {
        String result = "";
        MessageDigest sha;
        try {
            sha = MessageDigest.getInstance("SHA-256");
            sha.update(input.getBytes());
            result = HexUtil.bytesToHex(sha.digest());
        } catch (NoSuchAlgorithmException e) {
            GA.crypto.error("CryptoHash: Hash data fail", e);
        }
        return result.toUpperCase();
    }

    public String MD5Hash(String input){
        String result = "";
        MessageDigest sha;
        try {
            sha = MessageDigest.getInstance("MD5");
            sha.update(input.getBytes());
            result = HexUtil.bytesToHex(sha.digest());
        } catch (NoSuchAlgorithmException e) {
            GA.crypto.error("CryptoHash: Hash data fail", e);
        }
        return result.toUpperCase();
    }
}
